/**
 * @(#) popOtherSubtractionMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭11 기타소득공제 팝업 Presentation Logic을 관리 
 */
package com.skt.common_hr.yearadjustment.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.BankListProxy;
	import com.skt.common_hr.yearadjustment.model.OtherSubtraction2014Proxy;
	import com.skt.common_hr.yearadjustment.model.vo.OtherSubtrationVO;
	import com.skt.common_hr.yearadjustment.view.components.popOtherSubtraction2014;
	
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	

	public class popOtherSubtraction2014Mediator extends Mediator
	{
		public static const NAME:String = "popOtherSubtraction2014Mediator"; 
		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;		
	

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function popOtherSubtraction2014Mediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			view.addEventListener(popOtherSubtraction2014.OkInput, onOkInput);
  			view.addEventListener(popOtherSubtraction2014.CancelInput, onCancelInput);
 			view._isEnable = v_enable;
 			loadBankList();
 			//loadOtherSubtractionTypeList();
 		}
 		
 		private function loadOtherSubtractionList():void{
 			var proxy:OtherSubtraction2014Proxy = facade.retrieveProxy(OtherSubtraction2014Proxy.NAME) as OtherSubtraction2014Proxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		private function loadBankList():void{
 			var proxy:BankListProxy = facade.retrieveProxy(BankListProxy.NAME) as BankListProxy;
 			proxy.loadBankList();
 		}
 		
/* 
 		private function loadOtherSubtractionTypeList():void{
 			var proxy:OtherSubtractionProxy = facade.retrieveProxy(OtherSubtractionProxy.NAME) as OtherSubtractionProxy;
 			proxy.loadOtherSubtractionType(v_year);
 		}
 		 */
 		private var Amounts:Array;
 		
 		private function onOkInput(event:Event):void{
 			
 			Amounts = new Array(0, 0, 0);
 			
 		 	var proxy:OtherSubtraction2014Proxy = facade.retrieveProxy(OtherSubtraction2014Proxy.NAME) as OtherSubtraction2014Proxy;
 			
 			var detailList:Array = new Array();
	 		for each(var rec:OtherSubtrationVO in view.otherSubtractionList)
	 		{
				var obj:Object = new Object();
				obj.FINCO = rec.FINCO;			//금융기관코드
				obj.ACCNO = rec.ACCNO;			//계좌번호
				obj.BETRG = rec.BETRG;			//금액
				obj.INSNM = rec.INSNM;			//연금저축 금융사명				
				
//				obj.PREIN = rec.PREIN;			//종(전)근무지
//				obj.PNSTY = rec.PNSTY;			//비용의 2차 범주
//				obj.PNSTT = rec.PNSTT;			
//				obj.INSNM = rec.INSNM;			//연금저축 금융사명
//				obj.BEGDA = DateUtil.toBasicString(rec.BEGDA);			//시작일
//				obj.ENDDA = DateUtil.toBasicString(rec.ENDDA);			//종료일
//	 			switch(rec.PNSTY) {
//	 				case "01":
//	 					Amounts[0] += rec.BETRG;
//	 					break;
//	 				case "02":
//	 					Amounts[1] += rec.BETRG;
//	 					break;
//	 				case "03":
//	 					Amounts[2] += rec.BETRG;
//	 					break;
//	 				default:
//	 					PsHrAlert.showMessage("정의되지 않은 코드가 발생했습니다.("+rec.PNSTY+")");
//	 					break;
//	 			}
	 			detailList.push(obj);
	 		}
 			proxy.save(Variables.yearPernr, v_year, detailList);
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popOtherSubtraction2014Mediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
 		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					OtherSubtraction2014Proxy.LOAD_OTHER_SUBTRACTION_COMPLETE,
					OtherSubtraction2014Proxy.LOAD_OTHER_SUBTRACTION_FAILED,
					OtherSubtraction2014Proxy.LOAD_OTHER_TYPE_LIST_COMPLETE,
					OtherSubtraction2014Proxy.LOAD_OTHER_TYPE_LIST_FAILED,
					OtherSubtraction2014Proxy.SAVE_OTHER_SUBTRACTION_COMPLETE,
					OtherSubtraction2014Proxy.SAVE_OTHER_SUBTRACTION_FAILED,
					BankListProxy.LOAD_BANK_LIST_COMPLETE,
					BankListProxy.LOAD_BANK_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case OtherSubtraction2014Proxy.LOAD_OTHER_SUBTRACTION_COMPLETE:
					view.otherSubtractionList = (note.getBody() as OtherSubtraction2014Proxy).otherSubtractionList;
					break;
				case OtherSubtraction2014Proxy.LOAD_OTHER_SUBTRACTION_FAILED:
					PsHrAlert.showMessage((note.getBody() as OtherSubtraction2014Proxy).msgStr);
					break;
				case OtherSubtraction2014Proxy.LOAD_OTHER_TYPE_LIST_COMPLETE:
					view.typeList = (note.getBody() as OtherSubtraction2014Proxy).otherTypeList;
					//loadOtherSubtractionList();
					break;
				case OtherSubtraction2014Proxy.SAVE_OTHER_SUBTRACTION_COMPLETE:
					onCancelInput(null); 
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);					
					break;
				case OtherSubtraction2014Proxy.SAVE_OTHER_SUBTRACTION_FAILED:
					PsHrAlert.showMessage((note.getBody() as OtherSubtraction2014Proxy).msgStr);
					break;
				case BankListProxy.LOAD_BANK_LIST_COMPLETE:
					view.bankList = (note.getBody() as BankListProxy).bankList;
					loadOtherSubtractionList();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
 		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():popOtherSubtraction2014
        {
            return viewComponent as popOtherSubtraction2014;
        }
	}
}